package com.woniu.hospital.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.woniu.hospital.domain.Bedinfo;
import com.woniu.hospital.domain.BedinfoVo;
import com.woniu.hospital.service.IBedinfoService;
import com.woniu.hospital.service.UserService;
import com.woniu.hospital.service.IDeptService;
import com.woniu.hospital.service.IPatientService;
import com.woniu.user.domain.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 床位信息表Controller
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@RestController
@RequestMapping("/bedinfo")
public class BedinfoController extends BaseController
{
    @Autowired
    private IBedinfoService bedinfoService;
    @Autowired
    private IDeptService iDeptService;
    @Autowired
    private IPatientService iPatientService;

    @Autowired
    private UserService userService;

    /**
     * 查询床位信息表列表
     */
    @RequiresPermissions("hospital:bedinfo:list")
    @GetMapping("/list")
    public TableDataInfo list(Bedinfo bedinfo)
    {
        startPage();
        List<Bedinfo> list = bedinfoService.selectBedinfoList(bedinfo);
        for (Bedinfo item:list) {
             String patientName = userService.getPatientById(item.getPatientId()).getPatientName();
            item.setPatientName(patientName);
        }
        return getDataTable(list);
    }


    @RequiresPermissions("hospital:bedinfo:listYan")
    @GetMapping("/listYan")
    public TableDataInfo listYan(BedinfoVo bedinfoVo) {
        startPage();
        Bedinfo bedinfo = new Bedinfo();
        bedinfo.setBedId(bedinfoVo.getBedId());
        bedinfo.setBedLocation(bedinfoVo.getBedLocation());
        bedinfo.setBedName(bedinfoVo.getBedName());
        bedinfo.setBedPrice(bedinfoVo.getBedPrice());
        bedinfo.setBedStatus(bedinfoVo.getBedStatus());
        List<Bedinfo> list = bedinfoService.selectBedinfoList(bedinfo);

        List<BedinfoVo> list1 = new ArrayList<BedinfoVo>();
        for (Bedinfo bedinfo1 : list) {
            BedinfoVo bedinfoVo1 = new BedinfoVo();
            bedinfoVo1.setBedId(bedinfo1.getBedId());
            bedinfoVo1.setBedLocation(bedinfo1.getBedLocation());
            bedinfoVo1.setBedName(bedinfo1.getBedName());
            bedinfoVo1.setBedPrice(bedinfo1.getBedPrice());
            bedinfoVo1.setBedStatus(bedinfo1.getBedStatus());
            if(bedinfo1.getPatientId() != null){
                bedinfoVo1.setPatientId(bedinfo1.getPatientId());
                bedinfoVo1.setPatientName(iPatientService.selectById(bedinfo1.getPatientId()).getPatientName());
            }
            list1.add(bedinfoVo1);
        }

        Patient patientByNameYan = iPatientService.getPatientByNameYan(bedinfoVo.getPatientName());
        List<BedinfoVo> list2 = new ArrayList<BedinfoVo>();
       if(bedinfoVo.getPatientName()!=null){
           for (BedinfoVo vo : list1) {
                   if (vo.getPatientId() == patientByNameYan.getPatientId()){
                       list2.add(vo);
               }
           }
       }else{
           for (BedinfoVo vo : list1) {
                   list2.add(vo);
           }
       }

        return getDataTable(list2);
    }
    /**
     * 导出床位信息表列表
     */
    @RequiresPermissions("hospital:bedinfo:export")
    @Log(title = "床位信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bedinfo bedinfo)
    {
        List<Bedinfo> list = bedinfoService.selectBedinfoList(bedinfo);
        ExcelUtil<Bedinfo> util = new ExcelUtil<Bedinfo>(Bedinfo.class);
        util.exportExcel(response, list, "床位信息表数据");
    }

    /**
     * 获取床位信息表详细信息
     */
    @RequiresPermissions("hospital:bedinfo:query")
    @GetMapping(value = "/{bedId}")
    public AjaxResult getInfo(@PathVariable("bedId") Long bedId)
    {
        return success(bedinfoService.selectBedinfoByBedId(bedId));
    }

    @RequiresPermissions("hospital:allDepartmentListYan")
    @GetMapping("allDepartmentListYan")
    public AjaxResult allDepartmentListYan()
    {
        return success(iDeptService.selAllDept());
    }

    @RequiresPermissions("hospital:bedinfo:query")
    @GetMapping("/allDepartmentBedListYan/{allDepartmentbedLocation}")
    public AjaxResult allDepartmentBedListYan(@PathVariable("allDepartmentbedLocation") String departmentName)
    {
        return success(bedinfoService.selectAllBedByDepartmentName(departmentName));
    }


    /**
     * 新增床位信息表
     */
    @RequiresPermissions("hospital:bedinfo:add")
    @Log(title = "床位信息表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bedinfo bedinfo)
    {
        return toAjax(bedinfoService.insertBedinfo(bedinfo));
    }

    @RequiresPermissions("hospital:bedinfo:add")
    @Log(title = "床位信息表", businessType = BusinessType.INSERT)
    @PostMapping("/bedinfoyan")
    public AjaxResult addBedinfoYan(@RequestBody BedinfoVo bedinfoVo)
    {
        Bedinfo bedinfo = new Bedinfo();
        bedinfo.setBedLocation(bedinfoVo.getBedLocation());
        bedinfo.setBedName(bedinfoVo.getBedName());
        bedinfo.setBedPrice(bedinfoVo.getBedPrice());
        bedinfo.setBedStatus("待使用");
        return toAjax(bedinfoService.insertBedinfo(bedinfo));
    }
    /**
     * 修改床位信息表
     */
    @RequiresPermissions("hospital:bedinfo:edit")
    @Log(title = "床位信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bedinfo bedinfo)
    {
        return toAjax(bedinfoService.updateBedinfo(bedinfo));
    }

    /**
     * 删除床位信息表
     */
    @RequiresPermissions("hospital:bedinfo:remove")
    @Log(title = "床位信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bedIds}")
    public AjaxResult remove(@PathVariable Long[] bedIds)
    {
        return toAjax(bedinfoService.deleteBedinfoByBedIds(bedIds));
    }

}
